/**
* ...
* @author Default
* @version 0.1
*/

package bdz.dms.vo{
	import com.adobe.crypto.SHA1;
	
	
	public class UserVO extends CoreDataVO {
		private var _permissions:Object;
		private var propdelete:Array;
		function UserVO(xm:XML = null){	
			super(xm);
		}
		override protected function initialize():void {
			var counter:Number = 0;	
			_properties = new Array();
			if(written){
				for each(var o:* in _localxml.properties.property){
					_properties[counter] = new Object();
					_properties[counter].usermeta_id = o.usermeta_id;
					_properties[counter].meta_key = o.meta_key;
					_properties[counter].meta_value = o.meta_value;
					counter++;
				}
				for(var i:Number = 0;i<_properties.length;i++){
					if(_properties[i].meta_key == "permissions"){
						if(_properties[i].meta_value && _properties[i].meta_value != ""){
							applyPermissions(_properties[i].meta_value);
						}
					}
				}
			}else{
				_permissions = new Object();
			}
			
		}
		private function applyPermissions(str:String):void{
			_permissions = new Object();
			var arr = str.split(",");
			for each(var pair:String in arr){
				_permissions[pair.split(":")[0]] = Boolean(Number(pair.split(":")[1]));
			}
		}
		/*
		 * read-only
		 */
		public function get user_id():Number{
			return _localxml.user_id;
		}
		public function get sessionid():String{
			return _localxml.token;
		}
		public function get displayName():String{
			return _localxml.displayname;
		}
		public function set displayName(s:String):void{
			_localxml.displayname = s;
		}
		public function set email(s:String):void{
			_localxml.email = s;
		}
		public function get email():String{
			return _localxml.email;
		}
		public function get userName():String{
			return _localxml.username;
		}
		public function set userName(uname:String):void{
			_localxml.username = uname;
		}
		public function get password():String {
			return _localxml.password;
		}
		public function set password(p:String):void{
			_localxml.password = p;
		}
		private function getPermissions():String{
			var ret:String = "";
			for(var prop in _permissions){
				ret += prop+":"+Number(_permissions[prop])+",";
			}
			ret = ret.substr(0, ret.length-1)
			return ret;
		}
		public function assignPermission(key:String, val:Boolean):void{
			_permissions[key] = val;
		}
		public function checkPermission(key:String):Boolean{
			return _permissions[key];
		}
		public function addProperty(key:String, value:*):void{
			var o={};
			o[key] = value;
			_properties.push(o);
		}
		public function removeProperty(pid:Number):void{
			if(propdelete == null){
				propdelete = new Array();
				_localxml.deleteprops = "";
			}
			for (var i:Number=0;i<_properties.length;i++){
				if(_properties[i].usermeta_id == pid){
					deletePropIndex(i);
				}
			}
		}
		private function deletePropIndex(indx:Number):void{
			var xnode = <todelete/>;
			xnode.usermeta_id = _properties[indx].usermeta_id;
			propdelete.push(xnode);
			_properties.splice(indx,1);
		}
		override public function toXML():XML{
			var xvalue:XML = <dms/>;
			xvalue.user_id = _localxml.user_id;
			xvalue.username = _localxml.username;
			xvalue.displayname = _localxml.displayname;
			xvalue.email = _localxml.email;
			xvalue.password = _localxml.password;
			
			xvalue.properties = "";
			if(_properties.length < 1){
				_properties[0] = {meta_key:"permissions", meta_value:getPermissions()};
			}
			for(var i:Number = 0;i<_properties.length;i++){
				var xnode:XML = <property/>
				if(_properties[i].usermeta_id){
					xnode.usermeta_id = _properties[i].usermeta_id;
				}		
				xnode.meta_key = _properties[i].meta_key;
				xnode.meta_value = (_properties[i].meta_key == "permissions") ? getPermissions() : _properties[i].meta_value;
				xvalue.properties.property[i] = xnode;
			}
			
			if(propdelete){
				xvalue.deleteprops = ""
				for each(var xx:XML in propdelete){
					xvalue.deleteprops.todelete[i] = xx;
				}
			}
			return xvalue;
		}
		
	}
	
}